package com.auxgroup.hr.front.site.co.process;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.enums.WfTypeEnum;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.PsCPersonalData;
import com.auxgroup.hr.front.dao.domain.PsCWfXfrHdr;
import com.auxgroup.hr.front.service.personnal.PersonalDataService;
import com.auxgroup.hr.front.service.process.WfXfrHdrService;
import com.auxgroup.hr.front.site.co.BaseWfCO;
import com.auxgroup.hr.front.site.inteceptor.SessionGain;
import com.auxgroup.hr.front.site.vo.process.WfXfrHdrRespVO;
import com.auxgroup.hr.front.site.vo.process.WfXfrHdrSaveQueVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 工作调动审批流程
 * @author wangchangyun
 * @version 创建时间：180601 
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/wfXfrHdr")
public class WfXfrHdrCO extends BaseWfCO {

	@Autowired
	private WfXfrHdrService wfXfrHdrService;

	@Autowired
	private PersonalDataService personalDataService;

	@Override
	protected Set<String> getAuthFileds() {
		return new HashSet<>(Arrays.asList(
				"cXfrJoblevel", "cXfrJoblevelP", "cXfrDt", "action",
				"actionReason"
//				, "cXfrBusUtil", "cXfrDeptedescr", "cXfrJobcodeDesc",
//				"cXfrJoblvDescr", "cXfrJoblvpDescr", "cOtherExplain"
		));
	}
	@GetMapping(value = "/getXfrInfo")
	public BaseResponseVO getOne(String cTransactionNbr,@SessionGain("user") UserDTO user) {
		logger.info("getXfrInfo param= {}", cTransactionNbr);
		PsCWfXfrHdr wfXfrHdr = wfXfrHdrService.getWfXfrHdr(cTransactionNbr);
		if (wfXfrHdr == null) {
			return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
		}

		WfXfrHdrRespVO respVo = objectMapper.map(wfXfrHdr, WfXfrHdrRespVO.class);
		//调动类型
		respVo.setRsContChgTypeValue(getDictValue("RS_CONT_CHG_TYPE", wfXfrHdr.getRsContChgType()));

		//设置部门全称
		respVo.setDetpDescr(sysDictService.getDetpFullName(wfXfrHdr.getSetid(), wfXfrHdr.getDeptid()));
		//岗位名称
//		respVo.setJobcodeDescr(sysDictService.getJobDesc(wfXfrHdr.getSetid(),wfXfrHdr.getJobcode()));
		//导师名字
		if(StringUtils.isNotEmpty(wfXfrHdr.getcEmplid2())) {
			PsCPersonalData daoshi = personalDataService.getPersonalData(wfXfrHdr.getcEmplid2());
			if (daoshi != null) {
				respVo.setcEmplid2Name(daoshi.getName());
			}
		}
		fillBaseWfInfo(user, wfXfrHdr, respVo);

		return buildRespone(respVo);
	}

	@PostMapping(value="/saveXfr")
	public BaseResponseVO saveWfXfrHdr(WfXfrHdrSaveQueVO requestVO, BindingResult binding, @SessionGain("user") UserDTO user){
		logger.info("PrcHdr save param:{}",requestVO);
		// 表单校验
		super.assertValidation(binding);
		PsCWfXfrHdr terHdr = wfXfrHdrService.getWfXfrHdr(requestVO.getcTransactionNbr());
		if(terHdr==null){
			return buildRespone(BaseRetCode.CODE_ERROR_PROCESS_NOT_EXIST);
		}

		//设置数据
		Set<String> fs = super.getEditableFields(terHdr, user);
		if(fs.contains("cXfrDt"))terHdr.setcXfrDt(requestVO.getcXfrDt());
		if(fs.contains("actionReason"))terHdr.setActionReason(requestVO.getActionReason());
		//保存数据
		wfXfrHdrService.save(terHdr);
		//审批流程
		return requestWs(WfTypeEnum.WF_STYPE_XFR.getType(), requestVO, user);
	}




	@GetMapping(value = "/xfrReasons")
	public BaseResponseVO xfrReasons() {
		logger.info("xfrReasons");
		List<Object[]> reasons =sysDictService.getActionResons(WfTypeEnum.WF_STYPE_XFR.getType());
		return buildRespone(reasons, false);
	}
//
//	/**
//	 * 单独处理actionReason字段
//	 */
//	private void buildDictValue(WfXfrHdrRespVO respVo){
//		List<Object[]> resons =sysDictService.getActionResons(WfTypeEnum.WF_STYPE_XFR.getType());
//		for (Object[] str : resons) {
//			if(str[0].equals(respVo.getActionReason())){
//				respVo.setActionReasonValue(String.valueOf(str[1]));
//				return;
//			}
//		}
//
//	}
}
